﻿using Common.Http;
using Common.Msg;
using CommonData.Config;
using CToolsWin.DAL;
using HtmlAgilityPack;
using Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace CToolsWin.BLL
{
    public class BLLITellYouCategory : CToolsWin.IBLL.IGrab
    {
        #region 获取ITellYou类别信息
        private DALITellYouCategory dal = new DALITellYouCategory();

        /// <summary>
        /// 初始化表
        /// </summary>
        public void InitTable()
        {
            try
            {
                //初始化表
                Console.WriteLine("{0} ITellYou类别表初始化 - -", DateTime.Now);
                dal.InitTable();
                //获取商品分类信息
                Console.WriteLine("{0} ITellYou类别开始抓取 - -", DateTime.Now);
                Crawler();
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("初始化数据出现错误", this.GetType(), ex);
            }
        }
        /// <summary>
        /// 爬虫
        /// </summary>
        public void Crawler()
        {
            try
            {
                //获取html
                string html = HttpHelper.Get("http://www.itellyou.cn/", Encoding.UTF8, null, null,
                    new Dictionary<string, string>() { { "Cookie", SystemConfig.ITellYouCookie } });//读取数据

                //解析
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes("//*[@class='panel-title']/a");
                List<ITellYouCategory> list = new List<ITellYouCategory>();
                foreach (var node in nodeList)
                {
                    ITellYouCategory m = new ITellYouCategory();
                    m.CategoryLevel = 1;
                    m.Code = node.Attributes["data-menuid"].Value;
                    m.Name = node.InnerText;
                    m.ParentCode = "accordion";
                    m.Url = "http://www.itellyou.cn/Category/Index";
                    m.CreatedTime = DateTime.Now;
                    list.Add(m);
                    list.AddRange(GetITellYouCategory("http://www.itellyou.cn/Category/Index", 2, m.Code));
                }
                dal.Save(list);
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("获取内容出现错误", this.GetType(), ex);
            }
        }
        public static List<ITellYouCategory> GetITellYouCategory(string url, int level, string parent)
        {
            try
            {
                //获取html
                string html = HttpHelper.Post(HttpHelper.CreateHttpWebRequest(url: url, strCookie: SystemConfig.ITellYouCookie,
                    accept: "*/*",
                    acceptEncoding: "gzip, deflate",
                    acceptLanguage: "zh-CN,zh;q=0.8",
                    connection: "keep-alive",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    xRequestedWith: "XMLHttpRequest",
                    host: "www.itellyou.cn",
                    origin: "http://www.itellyou.cn",
                    referer: "http://www.itellyou.cn/")
                    , Encoding.UTF8, new Dictionary<string, string>() { { "id", parent } });//读取数据

                List<BaseITellYouCategory> listC = Common.Serialize.JsonHelper.ToObject<List<BaseITellYouCategory>>(html);

                List<ITellYouCategory> list = new List<ITellYouCategory>();
                foreach (var C in listC)
                {
                    ITellYouCategory m = new ITellYouCategory();
                    m.CategoryLevel = level;
                    m.Code = C.Id;
                    m.Name = C.name;
                    m.ParentCode = parent;
                    m.Url = url;
                    m.CreatedTime = DateTime.Now;
                    list.Add(m);
                }
                return list;
            }
            catch (Exception ex)
            {
                Log.GetLog.Error("获取内容出现错误", typeof(BLLITellYouCategory), ex);
            }
            return null;
        }
        #endregion
    }
    class BaseITellYouCategory
    {
        public string Id { get; set; }

        public string name { get; set; }
    }
}
